/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.broker.controller.operations;

import org.ourgrid.broker.commands.SchedulerData;
import org.ourgrid.broker.scheduler.extensions.GenericTransferHandle;
import org.ourgrid.broker.scheduler.extensions.IncomingHandle;
import org.ourgrid.broker.scheduler.workqueue.xmlcreator.AcceptTransferXMLCreator;
import org.ourgrid.broker.scheduler.workqueue.xmlcreator.CancelTransferXMLCreator;
import org.ourgrid.common.interfaces.to.GridProcessExecutionResult;
import org.ourgrid.common.interfaces.to.GridProcessHandle;

import br.edu.ufcg.lsd.commune.identification.DeploymentID;

/**
 * This class implements the operation of getting a file from a remote
 * directory.
 */
public class GetOperation extends FileTransferOperation {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IncomingHandle handle;
	
	public GetOperation(GridProcessHandle replicaHandle, long requestID,
			String workerID, String localFilePath, String remoteFilePath,
			String transferDescription, GridProcessExecutionResult gridResult) {
		
		super( replicaHandle, requestID, workerID, localFilePath, remoteFilePath, gridResult);
		
		DeploymentID workerDeploymentID = new DeploymentID(workerID);
		
		this.handle = new IncomingHandle(localFilePath, remoteFilePath, 0, transferDescription,
				workerDeploymentID.getContainerID().toString());
		
	}

	/* (non-Javadoc)
	 * @see org.ourgrid.broker.controller.operations.Operation#run()
	 */
	public void run() throws OperationException {
		acceptTransfer();
	}

	/**
	 * Accepts the transfer associated to this operation
	 */
	private void acceptTransfer() {
		
		getGridResult().getGetOperationTransferTime(this).setInitTime();
		setTransferActive(true);
		
		IncomingHandle handle = (IncomingHandle) getHandle();
		
		SchedulerData atData = new SchedulerData(getXmlCreator(AcceptTransferXMLCreator.class).
				getXML(handle.getId(), handle.getLocalFileName(), handle.getDestinationFileName(), 
						handle.getDescription(), handle.getSenderContainerID(), handle.getFileSize()));
		
		getCollector().addData(atData);
	}


	/* (non-Javadoc)
	 * @see org.ourgrid.broker.controller.operations.FileTransferOperation#cancelFileTransfer()
	 */
	public void cancelFileTransfer() {
		if ( isTransferActive() ) {
			
			IncomingHandle handle = (IncomingHandle) getHandle();
			
			SchedulerData ctData = new SchedulerData(getXmlCreator(CancelTransferXMLCreator.class).
					getXML(handle.getId(), handle.getLocalFileName(), handle.getDestinationFileName(), 
							handle.getDescription(), handle.getOppositeID(), handle.getFileSize(), false));

			getCollector().addData(ctData);
		}
	}


	@Override
	public GenericTransferHandle getHandle() {
		return handle;
	}


	public void setHandle(IncomingHandle handle) {
		this.handle = handle;
	}
}
